﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Organizer.pages
{
    public partial class TvShows : System.Web.UI.Page
    {
        #region Properties
  private BLOrganizer.BLTvShows BLTvShows;
        private int TvShowId { get { return Int32.Parse(hfTvShowId.Value.ToString()); } set { hfTvShowId.Value = value.ToString(); } }
        private int UserId { get { return Int32.Parse(Page.RouteData.Values["UserId"].ToString()); } }
        #endregion

        #region Events
  protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                LoadTvShows();
            }
            catch (Exception ex)
            {
                FeedBackMsg(ex.Message.ToString(), (int)controls.AlertTypes.Error);
            }
        }
        protected void grdTvShows_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (BLTvShows == null) { BLTvShows = new BLOrganizer.BLTvShows(); }

                switch (e.CommandName.ToString())
                {
                    case "Editar":
                        DataTable TvShow = BLTvShows.GetTvShowById(Int32.Parse(e.CommandArgument.ToString()));
                        txtName.Text = TvShow.Rows[0]["Name"].ToString();
                        txtEpisodeNumber.Text = TvShow.Rows[0]["EpisodeNumber"].ToString();
                        txtLastViewedEpisode.Text = TvShow.Rows[0]["LastViewedEpisode"].ToString();
                        ddlReleaseDay.SelectedValue = TvShow.Rows[0]["ReleaseDay"].ToString();
                        txtScore.Text = TvShow.Rows[0]["Score"].ToString();
                        txtSeasonNumber.Text = TvShow.Rows[0]["SeasonNumber"].ToString();
                        TvShowId = Int32.Parse(TvShow.Rows[0]["TvShowId"].ToString());
                        break;

                    case "Apagar":
                        BLTvShows.DeleteTvShow(Int32.Parse(e.CommandArgument.ToString()));
                        FeedBackMsg("TvShow deleted successfully.", (int)controls.AlertTypes.Sucess);
                        break;

                    default:
                        break;
                }
                LoadTvShows();
            }
            catch (Exception ex)
            {
                FeedBackMsg(ex.Message.ToString(), (int)controls.AlertTypes.Error);
            }
        }
        protected void grdTvShows_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                grdTvShows.PageIndex = e.NewPageIndex;
                LoadTvShows();
            }
            catch (Exception ex)
            {
                FeedBackMsg(ex.Message.ToString(), (int)controls.AlertTypes.Error);
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (BLTvShows == null) { BLTvShows = new BLOrganizer.BLTvShows(); }

                if (TvShowId.Equals(0))
                {
                    BLTvShows.AddTvShow(UserId, txtName.Text, Int32.Parse(txtEpisodeNumber.Text), Int32.Parse(txtSeasonNumber.Text), Int32.Parse(txtLastViewedEpisode.Text), ddlReleaseDay.SelectedValue, txtScore.Text);
                    FeedBackMsg("TvShow added successfully.", (int)controls.AlertTypes.Sucess);
                }
                else
                {
                    BLTvShows.UpdateTvShow(TvShowId, txtName.Text, Int32.Parse(txtEpisodeNumber.Text), Int32.Parse(txtSeasonNumber.Text), Int32.Parse(txtLastViewedEpisode.Text), ddlReleaseDay.SelectedValue, txtScore.Text);
                    FeedBackMsg("TvShow updated successfully.", (int)controls.AlertTypes.Sucess);
                }
                LoadTvShows();
                ClearFields();
                TvShowId = 0;
            }
            catch (Exception ex)
            {
                FeedBackMsg(ex.Message.ToString(), (int)controls.AlertTypes.Error);
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                ClearFields();
                TvShowId = 0;
            }
            catch (Exception ex)
            {
                FeedBackMsg(ex.Message.ToString(), (int)controls.AlertTypes.Error);
            }
        }
        #endregion

        #region Methods
  private void ClearFields()
        {
            try
            {
                txtEpisodeNumber.Text = string.Empty;
                txtLastViewedEpisode.Text = string.Empty;
                txtName.Text = string.Empty;
                txtScore.Text = string.Empty;
                txtSeasonNumber.Text = string.Empty;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void LoadTvShows()
        {
            try
            {
                if (BLTvShows == null) { BLTvShows = new BLOrganizer.BLTvShows(); }
                grdTvShows.DataSource = BLTvShows.GetTvShowsByUser((int)UserId);
                grdTvShows.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void FeedBackMsg(string Text, int AlertType)
        {
            try
            {
                Main Mn = (Main)Page.Master;
                Mn.FeedBackMsg(Text, AlertType);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}